package pl.umk.mat.fred.unicore.urc.storagemanagement.tree;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.w3.x2005.x08.addressing.EndpointReferenceType;

import de.fzj.unicore.rcp.identity.IdentityActivator;
import de.fzj.unicore.uas.client.StorageClient;
import de.fzj.unicore.uas.util.AddressingUtil;
import de.fzj.unicore.wsrflite.xfire.ClientException;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.GridNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.Node;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.RootNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.StorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.utils.TreeUtils;

public class ContentProviderImplementation implements ITreeContentProvider {

	private TreeViewer viewer;
	
	public ContentProviderImplementation(TreeViewer viewer) {
		this.viewer = viewer;
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
	}

	@Override
	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
		// TODO Auto-generated method stub
	}

	@Override
	public Object[] getChildren(Object arg0) {
		if (arg0 instanceof GridNode) {
			GridNode gridNode = (GridNode) arg0;
			if ((viewer != null) && !((RootNode)viewer.getInput()).areStoragesVisible()) {
				List<Node> result = new ArrayList<Node>();
				for (Node n : gridNode.getChildren()) {
					result.addAll(n.getChildren());
				}
				return TreeUtils.getMergedChildList(result).toArray();
			}
		}
		if (arg0 instanceof MultiplyCatalogNode) {
			MultiplyCatalogNode mcn = (MultiplyCatalogNode)arg0;
			return TreeUtils.getMergedChildList(mcn.getChildren()).toArray();
		}
		if (arg0 instanceof StorageNode) {
			StorageNode node = (StorageNode) arg0;
			StorageClient client = node.getStorage().getClient();
			try {
				if ((client == null) || !client.checkConnection()) {
					EndpointReferenceType epr = AddressingUtil.newEPR();
					epr.addNewAddress().setStringValue(node.getStorage().getUrl());
					client = new StorageClient(epr, IdentityActivator.getDefault().getUASSecProps(node.getStorage().getUrl()));
					node.getStorage().setClient(client);
				}
				if ((client == null) || !client.checkConnection()) {
					// nie udalo sie stworzyc klienta
					return null;
				}
			} catch (ClientException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (arg0 instanceof Node) {
			Node node = (Node) arg0;
			return node.getChildren().toArray();
		}
		return null;
	}

	@Override
	public Object[] getElements(Object arg0) {
		return getChildren(arg0);
	}

	@Override
	public Object getParent(Object arg0) {
		if (arg0 instanceof Node) {
			Node node = (Node) arg0;
			return node.getParent();
		}
		return null;
	}

	@Override
	public boolean hasChildren(Object arg0) {
		
		if (arg0 instanceof Node) {
			Node node = (Node) arg0;
			return node.getChildren().size() > 0;
		}
		return false;
	}
}
